﻿/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 5/8/2009
 * Time: 2:29 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using popLib;
using System.IO;

namespace QuickGenCode
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		
		ListBox lbx_item = new ListBox();
		TransparentForm bgForm;
		private string file_src = FileLib.getCurrentFile("TemplateCode.txt");
		Point previousLocation;
		
		public MainForm(string file_src)
		{			
			InitializeComponent();
			TransparentFormOption options = new TransparentFormOption( new Padding(15), true );
			options.ShowTitle = false;
			options.IsWindow = false;
			options.IsHandleOpacity = false;
			bgForm = FormLib.setTransparentForm( this, 255, options);
			
			APILib.SetWindowLong(bgForm.Handle, (-20), ( APILib.GetWindowLong(bgForm.Handle, (-20)) | 0x80) & ~0x40000);
			
			this.file_src = StringLib.isValid(file_src) ? file_src : this.file_src;
			reloadData();
			
			IntPtr hWnd = APILib.FindWindow( null, this.Text );			
			APILib.DefWindowProc( hWnd , APILib.WM_SHOWWINDOW, 0, 0);
			APILib.SendMessage( hWnd, APILib.WM_SETHOTKEY, APILib.HK_SHIFTZ, 0);
			previousLocation = this.Location;
		}
		
		void ActionEvent(object sender, EventArgs e)
		{
			
			switch( ((Control)sender).Name ){
				case "txt_input": //textChange
					if(lbx_list.FindString(txt_input.Text) >=0) lbx_list.SelectedIndex = lbx_list.FindString(txt_input.Text);
					break;
				case "lbx_list": //selectedIndexChange
					if(lbx_list.SelectedIndex == -1) return;
					txt_code.Text = (lbx_item.Items[ lbx_item.FindString( lbx_list.SelectedItem +"<<<" ) ].ToString().Replace("<<<", (char)(128)+"").Split( (char)(128) )[1]).Trim();
					break;
				case "btn_viewCode": //click
					this.Width = this.Width < 300 ? this.Width*2 : this.Width/2;
					break;
				case "btn_editor": //click
					System.Diagnostics.Process.Start( file_src );
					break;
				case "btn_reload": //click
					reloadData();
					break;
				case "MainForm": //Activate
					ShowWindow();
					break;
			}
		}
		
		public void reloadData(){
			lbx_list.Items.Clear();
			lbx_item.Items.Clear();
			
			if(!File.Exists(file_src))
				FileLib.writeFile( file_src, ">>>DemoCode<<<\r\n<div id=\"Demo\"></div>" );
						
			string[] itemCode = StringLib.splitByAny( FileLib.readFile(file_src), new string[]{">>>"} );
			
			
			foreach(string item in itemCode){
				if(item.Trim().Length>0){
					lbx_item.Items.Add( item );
					lbx_list.Items.Add( StringLib.splitByAny(item, new string[]{"<<<"})[0]  );
				}
			}
			
		}
		
		void doInsertText(bool backspace){
			Clipboard.SetText( txt_code.Text );
			
			//HideWindow(null, new EventArgs());
			//this.Visible = false;
			
			//Alt + Tab
			APILib.keybd_event (APILib.VK_Alt	, 0, 0, 0);
			APILib.keybd_event (APILib.VK_Tab , 0, 0, 0);
						
			APILib.keybd_event (APILib.VK_Tab	, 0, APILib.KEYEVENTF_KEYUP, 0);
			APILib.keybd_event (APILib.VK_Alt	, 0, APILib.KEYEVENTF_KEYUP, 0);
					
			
			//Backspace
			if(backspace){
				APILib.keybd_event (APILib.VK_Backspace , 0, 0, 0);
				APILib.keybd_event (APILib.VK_Backspace	, 0, APILib.KEYEVENTF_KEYUP, 0);
			}
			
			//Ctrl + V
			APILib.keybd_event (APILib.VK_Ctrl, 0, 0, 0);
			APILib.keybd_event (APILib.VK_V   , 0, 0, 0);
			
			APILib.keybd_event (APILib.VK_Ctrl, 0, APILib.KEYEVENTF_KEYUP, 0);
			APILib.keybd_event (APILib.VK_V, 0, APILib.KEYEVENTF_KEYUP, 0);
			
			this.Visible = true;
			HideWindow();
		}
		
		string hotkey = "Alt + Z";
		public void showFormByHotKey(){			
			if(hotkey == "Alt + Z"){				
				APILib.keybd_event (APILib.VK_Alt, 0, 0, 0);
				APILib.keybd_event (APILib.VK_Z, 0, 0, 0);
				APILib.keybd_event (APILib.VK_Z, 0, APILib.KEYEVENTF_KEYUP, 0);
				APILib.keybd_event (APILib.VK_Alt, 0, APILib.KEYEVENTF_KEYUP, 0);				
			}
			if(hotkey == "Shift + A"){
				APILib.keybd_event (APILib.VK_Shift, 0, 0, 0);
				APILib.keybd_event (APILib.VK_A, 0, APILib.KEYEVENTF_EXTENDEDKEY, 0);
				APILib.keybd_event (APILib.VK_A, 0, APILib.KEYEVENTF_KEYUP, 0);
				APILib.keybd_event (APILib.VK_Shift, 0, APILib.KEYEVENTF_KEYUP, 0);
			}
			if(hotkey == "Shift + B"){
				APILib.keybd_event (APILib.VK_Shift, 0, 0, 0);
				APILib.keybd_event (APILib.VK_B, 0, APILib.KEYEVENTF_EXTENDEDKEY, 0);
				APILib.keybd_event (APILib.VK_B, 0, APILib.KEYEVENTF_KEYUP, 0);
				APILib.keybd_event (APILib.VK_Shift, 0, APILib.KEYEVENTF_KEYUP, 0);
			}
			if(hotkey == "Shift + Z"){
				APILib.keybd_event (APILib.VK_Shift, 0, 0, 0);
				APILib.keybd_event (APILib.VK_Z, 0, APILib.KEYEVENTF_EXTENDEDKEY, 0);
				APILib.keybd_event (APILib.VK_Z, 0, APILib.KEYEVENTF_KEYUP, 0);
				APILib.keybd_event (APILib.VK_Shift, 0, APILib.KEYEVENTF_KEYUP, 0);
			}
		}
		
		
		void SubActionEvent(object sender, EventArgs e)
		{
			switch( ((Control)sender).Name ){
				case "lbx_list": //doubleClick
					doInsertText(false);
					break;
				case "txt_input": //Enter 
					txt_input.SelectAll();
					break;
				case "MainForm": //Deactivate
					HideWindow();
					break;
			}
		}
		
		void KeyDownEvent(object sender, KeyEventArgs e)
		{
			if(e.KeyValue == 40 && lbx_list.SelectedIndex+1 < lbx_list.Items.Count)
				lbx_list.SelectedIndex++;
			
			if(e.KeyValue == 38 && lbx_list.SelectedIndex > 0)
				lbx_list.SelectedIndex--;
			
			if(e.KeyValue == 27) HideWindow(  );
		}
		
		void MouseUpEvent(object sender, MouseEventArgs e)
		{
			switch( ((Control)sender).Name ){
				case "lbx_list":
					break;
				case "txt_input":
					break;
			}
		}
		
		
		void ShowWindow(){
			if(bgForm != null ){
				bgForm.Location = previousLocation;
			}
		}
		
		void HideWindow(){			
			if(bgForm != null && !bgForm.Child_Active ){
				FormLib.alert(bgForm.Child_Active + " - " + bgForm.Me_Active);
				bgForm.Location = new Point( -500, 200 );
			}
		}
		
		void MainFormLocationChanged(object sender, EventArgs e)
		{
			if( bgForm != null ) previousLocation = bgForm.Location;
		}
	}
}
